import { _decorator, Component, Node, Sprite, UITransform, Animation, AnimationClip, animation, SpriteFrame, input } from 'cc';


import { EntityManager } from './EntityManager';
import { IEntity } from '../Levels';
import EventManager from '../Runtime/EventManager';
import { WoodenSkeletonStateMachine } from '../Scripts/WoodenSkeleton/WoodenSkeletonStateMachine';
import DataManager from '../Runtime/DataManager';
import { DIRECTION_ENUM, ENTITY_STATE_ENUM, EVENT_ENUM } from '../Enums';

const { ccclass, property } = _decorator;



@ccclass('EnemyManager')
export class EnemyManager extends EntityManager {


    async init(params:IEntity) {
        super.init(params);

        EventManager.Instance.on(EVENT_ENUM.PLAYER_BORN, this.onChangeDirection, this)
        EventManager.Instance.on(EVENT_ENUM.PLAYER_MOVE_END, this.onChangeDirection, this)
        EventManager.Instance.on(EVENT_ENUM.ATTACK_ENEMY, this.onDead, this)

        this.onChangeDirection(true)

    }

    protected onDestroy(): void {
        super.onDestroy();
        EventManager.Instance.off(EVENT_ENUM.PLAYER_BORN, this.onChangeDirection)
        EventManager.Instance.off(EVENT_ENUM.PLAYER_MOVE_END, this.onChangeDirection)
        EventManager.Instance.off(EVENT_ENUM.ATTACK_ENEMY, this.onDead)
    }

    onChangeDirection(isInited: boolean = false) {
        if(this.state === ENTITY_STATE_ENUM.DEATH || !DataManager.Instance.player) return
        const { x: playerX, y: playerY } = DataManager.Instance.player;

        const disX = Math.abs(this.x - playerX);
        const disY = Math.abs(this.y - playerY);

        if (disX === disY && isInited) {
            return
        }

        if (playerX >= this.x && playerY <= this.y) {
            this.direction = disY > disX ? DIRECTION_ENUM.TOP : DIRECTION_ENUM.RIGHT;
        } else if (playerX <= this.x && playerY <= this.y) {
            this.direction = disY > disX ? DIRECTION_ENUM.TOP : DIRECTION_ENUM.LEFT;
        } else if (playerX <= this.x && playerY >= this.y) {
            this.direction = disY > disX ? DIRECTION_ENUM.BOTTOM : DIRECTION_ENUM.LEFT;
        } else if (playerX >= this.x && playerY >= this.y) {
            this.direction = disY > disX ? DIRECTION_ENUM.BOTTOM : DIRECTION_ENUM.RIGHT;
        }
    }



    onDead(id:string) {
        if(this.state === ENTITY_STATE_ENUM.DEATH) {
            return
        }

        if(this.id === id) {
            this.state = ENTITY_STATE_ENUM.DEATH;
        }

    }



}


